package com.rongshu.view.userInfo.fragment;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.discovery.bean.ArticleListResponse;
import com.rongshu.view.penpal.activity.PostDetailsActivity;
import com.rongshu.view.penpal.bean.PenpalArticleResponse;
import com.rongshu.view.userInfo.adapter.SharePostAdapter;
import com.rongshu.view.userInfo.iview.ShareArticleView;
import com.rongshu.view.userInfo.presenter.ShareArticlePresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class ShareInvationFragment extends BaseFragment<ShareArticlePresenter> implements ShareArticleView, SharePostAdapter.OnItemFunctionClickListener {
    @BindView(R.id.share_recyclerview)
    RecyclerView share_recyclerview;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.layout_empty)
    RelativeLayout layout_empty;
    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private SharePostAdapter mShareAdapter = null;
    //map
    private Map<String, Object> map = null;
    private int pageIndex = 1;
    private int pageSize = 10;
    //refresh,load
    private boolean isrefresh = true;
    //intent
    private Intent intent = null;
    private int userId;
    private int clickPosition;
    private int INTENT_DETAILS_REQUEST = 1001;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_share_invation_view;
    }

    @Override
    protected void initViews() {
        Intent intent = getActivity().getIntent();
        if (intent != null) {
            userId = intent.getIntExtra(Constant.USER_ID, 0);
        }
        //set presenter
        setPresenter(new ShareArticlePresenter(getActivity()), this);
        //layoutmanager
        manager = new LinearLayoutManager(getActivity());
        //adapter
        mShareAdapter = new SharePostAdapter(getActivity());
        mShareAdapter.setOnItemFunctionClickListener(this);
        //recyclerview
        share_recyclerview.setLayoutManager(manager);
        share_recyclerview.setAdapter(mShareAdapter);
        //触发自动刷新
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                isrefresh = true;
                pageIndex = 1;
                refreshLayout.resetNoMoreData();
                getMyShare();
                refreshlayout.finishRefresh(2000, false);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                isrefresh = false;
                pageIndex++;
                getMyShare();
                refreshlayout.finishLoadmore(2000, false);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    /**
     * 我的分享
     */
    private void getMyShare() {
        map = new HashMap<>();
        map.put("user_id", userId);
        map.put("type", 2);//分类(1文章 2帖子)
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        presenter.getMySharePost((int) map.get("user_id"),
                (int) map.get("type"),
                (int) map.get("pageIndex"),
                (int) map.get("pageSize"),
                Common.encryptionSign(map));
        map.clear();
    }

    @Override
    public void onClick(int position) {
        if (mShareAdapter != null) {
            clickPosition = position;
            PenpalArticleResponse.ResponseBean responseBean = mShareAdapter.getShareInfos().get(position);
            //发送的实体类
            intent = new Intent(getActivity(), PostDetailsActivity.class);
            intent.putExtra(Constant.POST_ID, responseBean.getTopic_id());
            intent.putExtra("showJoinIcon", true);
            startActivityForResult(intent, INTENT_DETAILS_REQUEST);
        }
    }

    @Override
    public void onDeleteClick(int position) {

    }

    @Override
    public void onMyShareArticle(ArticleListResponse shareInfo) {

    }

    @Override
    public void onMySharePost(PenpalArticleResponse shareInfo) {
        if (shareInfo.getErrCode() == 0) {
            List<PenpalArticleResponse.ResponseBean> list = shareInfo.getResponse();
            if (mShareAdapter != null) {
                if (isrefresh) {
                    refreshLayout.finishRefresh();
                    if (list == null || list.isEmpty()) {
                        refreshLayout.setVisibility(View.GONE);
                        layout_empty.setVisibility(View.VISIBLE);
                    } else {
                        refreshLayout.setVisibility(View.VISIBLE);
                        layout_empty.setVisibility(View.GONE);
                        mShareAdapter.setShareInfos(list);
                        mShareAdapter.notifyDataSetChanged();
                    }
                } else {
                    if (list == null || list.isEmpty()) {
                        refreshLayout.finishLoadmoreWithNoMoreData();
                    } else {
                        refreshLayout.finishLoadmore();
                        mShareAdapter.addShareInfos(list);
                        mShareAdapter.notifyDataSetChanged();
                    }
                }
            }
        } else {
            toast(shareInfo.getErrMsg());
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == INTENT_DETAILS_REQUEST) {
            int likeCount = data.getIntExtra(Constant.LIKE_COUNT, 0);
            int commentCount = data.getIntExtra(Constant.COMMENT_COUNT, 0);
            List<PenpalArticleResponse.ResponseBean> shareInfos = mShareAdapter.getShareInfos();
            shareInfos.get(clickPosition).setLikeCount(likeCount);
            shareInfos.get(clickPosition).setCommentCount(commentCount);
            mShareAdapter.notifyItemChanged(clickPosition);
        }
    }
}
